/**
 * excel导出文件
 */
layui.define(['zlContext','table', 'form'], function (exports) {
    "use strict";
    var $ = layui.$
        , form = layui.form
        , zlContext = layui.zlContext
        , table = layui.table
        , zlExcel = {
            downloadexcl: function (params, url) {
                // zlContext.showLoading();
                if (!url) {
                    url = layui.cache['contentPath'] + '/core/excel/export';
                }
                var html = ('<form id="downloadexcelform" action="' + url + '" method="post" style="display:none">');
                if ($("#downloadexcelform").length > 0) {
                    $("#downloadexcelform").remove();
                }
                if (params) {
                    params = JSON.stringify(params).replace(/"/g,"&quot;").replace(/'/g,"&apos;");
                    html += '<input name="model" value=\'' + params + '\'/>';
                }
                html += '</form>';
                $("body").append(html);
                $("#downloadexcelform").prop('action', url);
                $("#downloadexcelform").submit();
            }
            , excelpopform: function (params) {
                var htmlStr = [
                    '<div class="layui-fluid" lay-filter="divgridWorkflow" style="padding:20px 20px 20px 10px"> <form id ="excelForm" class="layui-form"  action="">'
                    , '	<div class="layui-form-item">'
                    , '		<label class="layui-form-label">标题</label>'
                    , '		<div class="layui-input-block">'
                    , '			<input type="text" name="filetitle" lay-verify="" placeholder="请输入标题,如无则为系统默认标题,导出CSV设置无效"  class="layui-input">'
                    , '		</div>'
                    , '	</div>'
                    , '	<div class="layui-form-item">'
                    , '		<label class="layui-form-label">文件名</label>'
                    , '		<div class="layui-input-block">'
                    , '			<input class="layui-input" type="text" name="fileName" lay-verify="" placeholder="请输入文件名,如无则为系统默认文件名">'
                    , '		</div>'
                    , '	</div>'
                    , '	<div class="layui-form-item">'
                    , '     <label class="layui-form-label">导出范围</label>'
                    , '     <div class="layui-input-block">'
                    , '         <input type="radio" name="datalist" value="0" title="当前页" checked="">'
                    , '         <input type="radio" name="datalist" value="1" title="选中数据">'
                    , '         <input type="radio" name="datalist" id="all" value="2" title="全部">'
                    , '     </div>'
                    , '	</div>'
                    , '	<div class="layui-form-item">'
                    , '     <label class="layui-form-label">文件格式</label>'
                    , '     <div class="layui-input-block">'
                    , '         <input type="radio" name="filetype" value=".xls" title="EXCEL文件" checked="">'
                    , '         <input type="radio" name="filetype" value=".csv" title="CSV文件" >'
                    , '     </div>'
                    , '	</div>'
                    , '	<div class="layui-hide">'
                    , '     <div class="layui-input-block">'
                    , '         <button id="confirm-btn" class="layui-btn fr mr10" lay-submit lay-filter="confirm-btn">确定</button>'
                    , '     </div>'
                    , '	</div>'
                    , '</form></div>'
                ]
                ,defaultsetting={
                    url:layui.cache['contentPath'] + '/core/excel/exportuserdef',
                    MAINTABLENAME_ZLFW:""
                }
                ,tableid = params.tableid
                , all = params.all==undefined ?true  : params.all
                ,checklist = tableid?table.checkStatus(tableid).data:params.checklist
                ,columns = tableid?{}:params.columns
                ,filetitle = params.filetitle?params.filetitle:''
                ,fileName = params.fileName?params.fileName:''
                ,curpagelist = tableid?layui.table.cache[tableid]:params.curpagelist;
                params = $.extend({}, defaultsetting, params);
                if(tableid){
                    table.eachCols(tableid, function(i3, item3){
                        if(item3.field && item3.type == 'normal' && !item3.hide){
                            columns[item3.title] = item3.field;     
                        }
                    });
                }
                if(!columns){
                    zlContext.errorMsg("缺少参数配置，请检查参数配置！");
                    return;
                }                
                layer.open({
                    type: 1,
                    title: '数据导出设置',
                    area: ['600px', '360px'],
                    btn: ['确定', '取消'],
                    content: htmlStr.join(''),
                    success: function (layero, index) {
                        if(filetitle){
                            $('input[name="filetitle"]').val(filetitle);
                        }                    
                        if(fileName){
                            $('input[name="fileName"]').val(fileName);
                        }
                        form.render();
                        if(!all){
                            $('#all').next(".layui-form-radio").hide();
                        }
                        form.on('submit(confirm-btn)', function (data) {
                            var newparam=data.field;
                            if(newparam.datalist=="0"){
                                newparam.list=curpagelist;
                            }else if(newparam.datalist=="1") {
                                newparam.list= checklist;
                                if(!checklist||checklist.length==0){
                                    zlContext.warnMsg("无数据选中,请先选择数据！");
                                    return false;
                                }
                            }
                            newparam.columns = columns;
                            newparam.MAINTABLENAME_ZLFW = params.MAINTABLENAME_ZLFW;
                            newparam = $.extend({}, params,newparam);
                            if(newparam.datalist=="2"&&(!newparam.sql)){
                                table.exportFileZl(tableid,newparam);
                            }else {
                                zlExcel.downloadexcl(newparam,params.url);
                            }
                            layer.close(index);                            
                            return false;
                        });
                    },
                    yes: function (layero, index) {
                        document.getElementById("confirm-btn").click();
                    }
                });
            }
        }
    exports("zlExcel", zlExcel);
});
